﻿using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace LegendHub.Crypt
{
    public static class AES
    {
        private static string _key;

        private static string _vector;

        public static string IV
        {
            get
            {
                return _vector;
            }
            set
            {
                _vector = value;
            }
        }

        public static string Key
        {
            get
            {
                return _key;
            }
            set
            {
                _key = value;
            }
        }

        static AES()
        {
            _key = ")(*djkhO&*Y*&hiu-1a2S3J0(i9-0&";
            _vector = "a+\\~fT,c;8%$=pkf";
        }

        /// <summary>
        /// AES加密
        /// </summary>
        /// <param name="plainStr">需要加密的字符串</param>
        /// <returns>加密后的字符串</returns>
        public static string AESEncrypt(string plainStr)
        {
            var bytes = Encoding.UTF8.GetBytes(Key);
            var numArray = Encoding.UTF8.GetBytes(IV);
            var bytes1 = Encoding.UTF8.GetBytes(plainStr);
            string base64String = null;
            try
            {
                using (var rijndael = Rijndael.Create())
                {
                    using (var memoryStream = new MemoryStream())
                    {
                        using (var cryptoStream = new CryptoStream(memoryStream, rijndael.CreateEncryptor(bytes, numArray), CryptoStreamMode.Write))
                        {
                            cryptoStream.Write(bytes1, 0, bytes1.Length);
                            cryptoStream.FlushFinalBlock();
                            base64String = Convert.ToBase64String(memoryStream.ToArray());
                        }
                    }
                }
            }
            catch
            {
                // ignored
            }
            return base64String;
        }

        /// <summary>
        /// AES加密（可选返回空字符串还是null）
        /// </summary>
        /// <param name="plainStr">需要加密的字符串</param>
        /// <param name="returnNull">是否返回null</param>
        /// <returns>加密后的字符串</returns>
        public static string AESEncrypt(string plainStr, bool returnNull)
        {
            var str = AESEncrypt(plainStr);
            if (returnNull)
            {
                return str;
            }
            if (str != null)
            {
                return str;
            }
            return string.Empty;
        }

        /// <summary>
        /// AES解密
        /// </summary>
        /// <param name="encryptStr">加密过的字符串</param>
        /// <returns>解密后的字符串</returns>
        public static string AESDecrypt(string encryptStr)
        {
            var bytes = Encoding.UTF8.GetBytes(Key);
            var numArray = Encoding.UTF8.GetBytes(IV);
            var numArray1 = Convert.FromBase64String(encryptStr);
            string str = null;
            try
            {
                using (var rijndael = Rijndael.Create())
                {
                    using (var memoryStream = new MemoryStream())
                    {
                        using (var cryptoStream = new CryptoStream(memoryStream, rijndael.CreateDecryptor(bytes, numArray), CryptoStreamMode.Write))
                        {
                            cryptoStream.Write(numArray1, 0, numArray1.Length);
                            cryptoStream.FlushFinalBlock();
                            str = Encoding.UTF8.GetString(memoryStream.ToArray());
                        }
                    }
                }
            }
            catch
            {
                // ignored
            }
            return str;
        }

        /// <summary>
        /// AES解密（可选返回空字符串还是null）
        /// </summary>
        /// <param name="encryptStr">加密过的字符串</param>
        /// <param name="returnNull">是否返回null</param>
        /// <returns>解密后的字符串</returns>
        public static string AESDecrypt(string encryptStr, bool returnNull)
        {
            var str = AESDecrypt(encryptStr);
            if (returnNull)
            {
                return str;
            }
            return str ?? string.Empty;
        }
    }
}